{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1=open('test.csv',\"r\",encoding=\"utf8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr=[]\n",
    "for line in f1:\n",
    "    row=line.split(\",\")\n",
    "    arr.append(row)\n",
    "f1.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['张三', '90', '80', '70', 'A', '及格\\n'],\n",
       " ['张三', '59', '80', '70', 'A', '及格\\n'],\n",
       " ['张三', '90', '3', '70', 'A', '及格\\n'],\n",
       " ['张三', '78', '80', '90', 'A', '及格\\n'],\n",
       " ['张三', '90', '80', '70', 'A', '及格']]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找语文优秀的人数\n",
    "aa= len([row[1] for row in arr if int(row[1])>=90])\n",
    "aa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'优秀': 0, '良好': 0, '及格': 0, '不及格': 0},\n",
       " {'优秀': 0, '良好': 0, '及格': 0, '不及格': 0},\n",
       " {'优秀': 0, '良好': 0, '及格': 0, '不及格': 0}]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "km=\"语文\",\"数学\",\"英语\"\n",
    "djrs=[]\n",
    "for i in range(3):\n",
    "    djrs.append({\"优秀\":0,\"良好\":0,\"及格\":0,\"不及格\":0})\n",
    "djrs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'优秀': 3, '良好': 0, '及格': 1, '不及格': 1},\n",
       " {'优秀': 0, '良好': 4, '及格': 0, '不及格': 1},\n",
       " {'优秀': 1, '良好': 0, '及格': 4, '不及格': 0}]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 循环统计\n",
    "for i in range(len(arr)):\n",
    "    for j in range(1,4):\n",
    "        score=int(arr[i][j])\n",
    "        # print(score)\n",
    "        if score>=90:\n",
    "            djrs[j-1]['优秀']+=1\n",
    "        elif 80<=score<90:\n",
    "            djrs[j-1]['良好']+=1\n",
    "        elif 60<=score<80:\n",
    "            djrs[j-1]['及格']+=1\n",
    "        elif score<60:\n",
    "            djrs[j-1]['不及格']+=1\n",
    "\n",
    "djrs\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def findyouxiu(s):\n",
    "   return s>=90\n",
    "scoreline={ \n",
    "    \"优秀\":lambda s: int(s)>=90,\n",
    "    \"良好\":lambda s: 80<=int(s)<90,\n",
    "    \"及格\":lambda s: 60<=int(s)<80,\n",
    "    \"不及格\":lambda s: int(s)<60           \n",
    "}\n",
    "project_index={\n",
    "    \"语文\":1,\n",
    "    \"数学\":2,\n",
    "    \"英语\":3\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "语文\n",
      "{'优秀': 3, '良好': 0, '及格': 1, '不及格': 1}\n",
      "数学\n",
      "{'优秀': 0, '良好': 4, '及格': 0, '不及格': 1}\n",
      "英语\n",
      "{'优秀': 1, '良好': 0, '及格': 4, '不及格': 0}\n"
     ]
    }
   ],
   "source": [
    "# len([row[1] for row in arr if int(row[1])>=90])\n",
    "# 切片统计\n",
    "for project,score in zip(km,djrs):\n",
    "    print(project)\n",
    "    for ss in score:\n",
    "        # print('project',project,'ss',ss)\n",
    "        index_i=project_index[project]\n",
    "        score[ss]= len([row[ index_i ] for row in arr if scoreline[ss](row[ index_i ]) ]) \n",
    "    print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['张三', '90', '80', '70', 'A', '及格\\n'],\n",
       " ['张三', '59', '80', '70', 'A', '及格\\n'],\n",
       " ['张三', '90', '3', '70', 'A', '及格\\n'],\n",
       " ['张三', '78', '80', '90', 'A', '及格\\n'],\n",
       " ['张三', '90', '80', '70', 'A', '及格']]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
